package Lee_code;

/**
 * @author blh
 * @date 2024年07月16日 12:04
 */
public class Lee_92_716 {
    public ListNode reverseBetween(ListNode head, int left, int right) {
        if (head == null) {
            return null;
        }
        ListNode dummyHead = new ListNode(-500);
        dummyHead.next = head;
        ListNode pre = dummyHead;
        ListNode last = dummyHead;
        // 先找到pre节点
        for (int i = 0; i < left - 1; i++) {
            pre = pre.next;
        }
        for (int i = 0; i < right+1; i++) {
            last = last.next;
        }
        ListNode cur, tail = last;
        cur = pre.next;
        for (int i = 0; i < right - left + 1; i++) {
            ListNode temp = cur.next;
            pre.next = cur;
            cur.next = tail;
            tail = cur;
            cur = temp;
        }

        return dummyHead.next;
    }
}
